package com.zhulong.saas.cloud.gateway.oauth2;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;

import java.io.Serializable;
import java.util.*;

public class FixResourceServerTokenServices implements ResourceServerTokenServices {

    private Map<String, Authentication> authenticationMap = new HashMap<>();

    {
        authenticationMap.put("123", new UsernamePasswordAuthenticationToken("ss", "N/A", Arrays.asList(new SimpleGrantedAuthority("admin"))));
    }

    @Override
    public OAuth2Authentication loadAuthentication(String s) throws AuthenticationException, InvalidTokenException {
        Authentication authentication = this.authenticationMap.get(s);
        if (authentication != null) {
            OAuth2Request oAuth2Request = new OAuth2Request(new HashMap<>(), "111", new ArrayList<>(), true, new HashSet<>(), new HashSet<>(), "", new HashSet<>(), new HashMap<>());
            return new OAuth2Authentication(oAuth2Request, this.authenticationMap.get(s));
        }
        return null;
    }

    @Override
    public OAuth2AccessToken readAccessToken(String s) {
        return new DefaultOAuth2AccessToken(s);
    }
}
